<template>
	<view class="container">
		<view class="logo"><u-image width="300" height="300" :src="logo"></u-image></view>
		<view><u-button style="width:70%" type="success" ripple @click="getWxCode">微信一键登录</u-button></view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			logo: '/static/qlzj.jpg',
			routePath: '',
			userInfo: {
				nickName: '',
				openid: '',
				avatar: '',
				sex: ''
			}
		};
	},
	methods: {
		getWxCode() {
			if (this.isWechat()) {
				// 截取地址中的code，如果没有code就去微信授权，如果已经获取到code了就直接把code传给后台获取openId
				window.location.href =
					'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxe283a89e01985b0d' +
					'&redirect_uri=' +
					encodeURIComponent('http://www.mksoft.cn/qlyg/#/pages/wxlogin/jump') +
					'&response_type=code&scope=snsapi_userinfo&state=' + encodeURIComponent(this.routePath)
					+'#wechat_redirect';
				// redirect_uri是授权成功后，跳转的url地址，微信会帮我们跳转到该链接，并且通过？的形式拼接code，这里需要用encodeURIComponent对链接进行处理。
				// 如果配置参数一一对应，那么此时已经通过回调地址刷新页面后，你就会再地址栏中看到code了。
				// http://127.0.0.1/pages/views/profile/login/login?code=001BWV4J1lRzz00H4J1J1vRE4J1BWV4q&state=1
			}
		},
		isWechat() {
			return String(navigator.userAgent.toLowerCase().match(/MicroMessenger/i)) === 'micromessenger';
		},
		getUrlCode(name) {
			return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ''])[1].replace(/\+/g, '%20')) || null;
		},
	},
	onLoad() {
		if (this.$Route.query.routePath) {
			if (this.$Route.query.routePath == '/pages/wxlogin/wxlogin') {
				this.routePath = '/pages/index/';
			}else{
				this.routePath = this.$Route.query.routePath;
			}
		}
	}
};
</script>

<style>
.logo {
	display: flex;
	justify-content: center;
}
</style>
